package com.auoem.radio.common.custview;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.widget.Toast;

import com.auctus.am.framework.utils.LogM;

/**
 * descript: 进度框，需在Activity中判断isfinish
 *
 * @HuRh create:2019/3/4
 */
public class ProgressUtil {
    /**
     * 进度提示框
     */
    private ProgressDialog progressDialog = null;

    /**
     * 显示加载进度提示框
     */

    private Activity mContext;

    public ProgressUtil(Activity context) {
        this.mContext = context;
//        LogMini.i("ProgressUtil", "ProgressUtil isFinishing: " + mContext.isFinishing());
    }

    public void showProgressDialog(String showMessage, OnKeyListener keyListener) {
//        if (mContext.isFinishing()) {
//            return;
//        }
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(mContext);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(true);
        }
        progressDialog.setMessage(showMessage);
        progressDialog.setOnKeyListener(keyListener);
        progressDialog.show();
    }

    public void showProgressDialog(String showMessage, boolean cancelable, DialogInterface.OnDismissListener listener) {
//        if (mContext.isFinishing()) {
//            return;
//        }
        cancelToast();
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(mContext);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(cancelable);
        }
        progressDialog.setMessage(showMessage);
        progressDialog.setOnDismissListener(listener);
        progressDialog.show();
    }

    public void showProgressDialog(String showMessage, DialogInterface.OnDismissListener listener) {
        showProgressDialog(showMessage, true, listener);
    }

    /**
     * 显示加载进度提示框
     */
    public void showProgressDialog(String showMessage) {
        if (mContext.isFinishing()) {
            return;
        }
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(mContext);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(true);
        }
        progressDialog.setMessage(showMessage);
        progressDialog.show();
    }

    /**
     * 显示加载进度提示框
     */
    public void showProgressDialog(String showMessage, boolean isCancelable) {
//        LogMini.i("ProgressUtil", "isFinishing: " + mContext.isFinishing());
        cancelToast();
        if (mContext.isFinishing()) {
            return;
        }//无效
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(mContext);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(isCancelable);
        }
        progressDialog.setMessage(showMessage);
        progressDialog.show();
    }


    public void updateProgressDialog(String showMessage) {
        if (mContext.isFinishing()) return;

        if (null == progressDialog || null == showMessage) return;

        progressDialog.setMessage(showMessage);
    }

    /**
     * 关闭进度提示框
     */
    public void cancelProgressDialog() {
        if (mContext.isFinishing()) {
            return;
        }
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.cancel();
            progressDialog = null;
            LogM.d("ProgressDialog","cancelProgressDialog");
        }
    }

    long tostShowTime = 0;

    Toast mToast;

    /**
     * @param strResource 字符串id
     */
    public void showToast(int strResource) {
        if (System.currentTimeMillis() - tostShowTime > 3000) {
            tostShowTime = System.currentTimeMillis();
            Toast.makeText(mContext, strResource, 1).show();
        }
    }

    /**
     * @param str 字符串
     */
    public void showToast(String str) {
//        if (System.currentTimeMillis() - tostShowTime > 3000) {
//            tostShowTime = System.currentTimeMillis();
//            mToast = Toast.makeText(mContext, str, 1);
//            mToast.show();
//        }
        cancelToast();
        mToast = Toast.makeText(mContext, str, Toast.LENGTH_SHORT);
        mToast.show();
    }

    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }


}
